package com.liushuang.waiwaiim.controller.activity;


import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.liushuang.waiwaiim.R;
import com.liushuang.waiwaiim.controller.fragment.ChatFragment;
import com.liushuang.waiwaiim.controller.fragment.ContactListFragment;
import com.liushuang.waiwaiim.controller.fragment.SettingFragment;

public class MainActivity extends AppCompatActivity {

    private RadioGroup mRadioGroup;
    private ChatFragment mChatFragment;
    private ContactListFragment mContactListFragment;
    private SettingFragment mSettingFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                Fragment fragment = null;
                switch (checkedId){
                    case R.id.rb_main_chat:
                        fragment = mChatFragment;
                        break;
                    case R.id.rb_main_contact:
                        fragment = mContactListFragment;
                        break;
                    case R.id.rb_main_setting:
                        fragment = mSettingFragment;
                        break;
                    default:
                        break;
                }

                //实现Fragment的切换
                switchFragment(fragment);
            }
        });
        mRadioGroup.check(R.id.rb_main_chat);
    }

    private void switchFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.id_fl_main, fragment).commit();
    }

    private void initData() {
        mChatFragment = new ChatFragment();
        mContactListFragment = new ContactListFragment();
        mSettingFragment = new SettingFragment();
    }

    private void initView() {
        mRadioGroup = (RadioGroup) findViewById(R.id.id_rg_main);
    }
}